package com.ving.test.mybatis01.statement;

import com.ving.test.mybatis.resultset.ResultSetHandler;
import com.ving.test.mybatis.util.DBUtil;
import com.ving.test.mybatis01.Configuration01;
import com.ving.test.mybatis01.binding.MapperMethod01;
import com.ving.test.mybatis01.resultset.ResultSetHandler01;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by liuweijie on 2019-11-19.
 */
public class StatementHandler01 {

    private Configuration01 configuration01;
    private ResultSetHandler01 resultSetHandler;


    public StatementHandler01(Configuration01 configuration) {
     this.configuration01=configuration;
     this.resultSetHandler =new ResultSetHandler01();
    }

    public <T> T query(MapperMethod01 mapperMethod01, String parameter) {
        Connection connection = null;
        try {
            connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(String.format(mapperMethod01.getSql(),Integer.parseInt((String.valueOf(parameter)))));
            preparedStatement.execute();

            return resultSetHandler.handle(preparedStatement,mapperMethod01);


        } catch (Exception e) {
            e.printStackTrace();
        }
        DBUtil.closeConnection(connection);
        return null;
    }
}
